package cn.moefly.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

@Slf4j
public abstract class SystemEnvironmentUtils {

    public static<T> boolean isStartupFromJar(Class<T> clazz) {
        //jar run： file:/mnt/e/SharePath/code/event-logger/build/libs/event-service.jar!/BOOT-INF/classes!/
        //ide/gradle run： /mnt/e/SharePath/code/event-logger/build/classes/java/main/
        var path = clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
        return path.startsWith("file:");
    }

    public static<T> String currentJarFolderPath(Class<T> clazz){
        return new ApplicationHome(clazz).getSource().getParentFile().getPath() + File.separator;
    }

    /**
     * win C:\Users\Name\AppData\Local\Temp\
     * @return
     */
    public static String systemTempFolder(){
        return System.getProperty("java.io.tmpdir");
    }


    /**
     * if start with jar: read the files below jar's path
     * else use tue dev files of the fileName, for example, file.yml will use file-dev.yml.
     */

    public static InputStream resourceConfigFileInputStream(String fileName){
        return resourceConfigFileInputStream(fileName,true);
    }
    /**
     * if start with jar: read the files below jar's path
     * else do as the name of useDevFileWhenNotStartFromJar
     *
     * @param useDevFileWhenNotStartFromJar will use tue dev files of the fileName, for example, file.yml will use file-dev.yml,
     *                                      otherwise will use the fileName directly
     *
     */
    public static InputStream resourceConfigFileInputStream(String fileName,boolean useDevFileWhenNotStartFromJar){
        boolean startupFromJar = SystemEnvironmentUtils.isStartupFromJar(SystemEnvironmentUtils.class);
        if (startupFromJar){
            log.info("Start with jar and use the {} file under the current directory",fileName);
            String filePath = SystemEnvironmentUtils.currentJarFolderPath(SystemEnvironmentUtils.class) + fileName;
            try {
                return new FileInputStream(filePath);
            } catch (FileNotFoundException e) {
                log.warn("File not found, use jar built-in development file of {}",fileName);
                return getDevConfigStream(fileName);
            }
        }

        if (useDevFileWhenNotStartFromJar){
            log.info("Non-jar startup, use built-in  development file of {}",fileName);
            return getDevConfigStream(fileName);
        } else {
            return SystemEnvironmentUtils.class.getResourceAsStream("/config/" + fileName);
        }
    }

    private static InputStream getDevConfigStream(String orginFileName){
        int lastIndexOfDot = orginFileName.lastIndexOf(".");
        var fileNameNoExtension = orginFileName.substring(0,lastIndexOfDot);
        var extension = orginFileName.substring(lastIndexOfDot);
        var devFileName = fileNameNoExtension + "-dev" + extension;
        log.error(devFileName);
        return SystemEnvironmentUtils.class.getResourceAsStream("/config/" + devFileName);
    }

}
